<?php
Prado::using('System.I18N.core.NumberFormat');
Prado::using('Application.common.common');
class ShoppingCart extends TPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Master->pageHead->Title = $this->Application->Parameters["WEBSITE_TITLE"] . " | Shopping Cart";
		
		if (!$this->IsPostBack)
		{
			$cartRecord = CartTempRecord::finder()->findByPk($this->Session->SessionID);
			if ($cartRecord instanceof CartTempRecord)
			{
				$this->populateCartDetails();
			}
			else
			{
				$this->Notice->Type = UserNoticeType::Notice;
				$this->Notice->Text = $this->Application->getModule("message")->translate("CART_EMPTY");
				$this->mainBox->Visible = false;
				$this->btnContinue2->Visible = true;
			}	
			
			$this->renderBoxShippingMethod();
		}

	}
	
	public function populateCartDetails()
	{
		$this->rptCart->DataSource = CartTempDetailRecord::finder()->findAllBysession_id($this->Session->SessionID);
		$this->rptCart->DataBind();
		
		$cartRecord = CartTempRecord::finder()->findByPk($this->Session->SessionID);
		$this->lblSubTotal->Text = Common::getFormattedValue($cartRecord->getSubtotalInSession());
		
		if (count($this->rptCart->Items)<=0)
		{
			$this->Notice->Type = UserNoticeType::Notice;
			$this->Notice->Text = $this->Application->getModule("message")->translate("CART_EMPTY");
			$this->mainBox->Visible = false;
			$this->btnContinue2->Visible = true;
		}		
	}

	public function renderBoxShippingMethod()
	{
		$limit = null;
		$offset = null;
		$sqlmap = $this->Application->Modules['sqlmap']->Client;
		$items = $sqlmap->queryForList("GetShippingMethodList", null,null,$offset,$limit);
		$this->ListShippingMethod->DataSource = $items;
		$this->ListShippingMethod->DataBind();	
	}
	
	protected function btnContinue_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("Index"));
	}
	
	protected function btnCheckOut_Clicked($sender, $param)
	{
		$this->Response->redirect($this->Service->ConstructUrl("shop.checkout.Shipping"));
	}
}

?>